home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / bin / geomutil / ucd / ucdtooff.c < prev    next >
C/C++ Source or Header  |  1993-11-04  |  9KB  |  342 lines

  1.  
  2. /* File:    anytoucd.c:
  3.    Author:    Charlie Gunn originally
  4.                 
  5.    read a OOGL object on stdin, and write ucd format on stdout.
  6. */
  7. #include "vec4.h"
  8. #include "geom.h"
  9. #include "ooglutil.h"
  10. #include "3d.h"
  11. #include "polylistP.h"
  12. #include "plutil.h"
  13. #include <stdio.h>
  14. #include "time.h"
  15.  
  16. /*
  17. #define DEBUG
  18. */
  19.  
  20. typedef struct {
  21.     int id;
  22.     HPoint3 v;
  23.     }  ucdvert;
  24.  
  25. typedef struct {
  26.     int id;
  27.     int m;
  28.     int n;
  29.     int vlist[4];
  30.     }  ucdpoly;
  31.  
  32. int
  33. getcellsize(str)
  34. char *str;
  35. {
  36.     if (strcmp(str,"line") == 0)  return(2);
  37.     else if (strcmp(str,"tri") == 0)  return(3);
  38.     else if (strcmp(str,"quad") == 0)  return(4);
  39.     else return(0);
  40. }
  41.  
  42. #define UCD_NORMAL    1
  43. #define UCD_RGBA    2
  44. #define UCD_RGB        3
  45. #define UCD_Z        4
  46. #define UCD_NUMDATAFIELDS    8
  47.  
  48. int 
  49. gettype(str)
  50. char *str;
  51. {
  52.     if (strcmp(str, "normal") == 0) return UCD_NORMAL; 
  53.     else if (strcmp(str, "rgba") == 0) return UCD_RGBA; 
  54.     else if (strcmp(str, "rgb") == 0) return UCD_RGB; 
  55.     else return(0);
  56. }
  57.  
  58. int
  59. nodeidtoindex(id, v, num_nodes)
  60. int id;
  61. ucdvert *v;
  62. int num_nodes;
  63. {
  64.     int i;
  65.     for (i=0; i<num_nodes; ++i)    
  66.     if (v[i].id == id) return(i);
  67.     return(-1);
  68. }
  69.  
  70. int
  71. cellidtoindex(id, p, num_cells)
  72. int id;
  73. ucdpoly *p;
  74. int num_cells;
  75. {
  76.     int i;
  77.     for (i=0; i<num_cells; ++i)    
  78.     if (p[i].id == id) return(i);
  79.     return(-1);
  80. }
  81. main(argc, argv)    int argc; char **argv;
  82. {
  83.     FILE *fp = stdin;
  84.     int num_nodes, 
  85.     num_node_data_comp,
  86.     *node_data_comp = NULL,
  87.     num_cells, 
  88.     num_cell_data_comp,
  89.     num_model_data,
  90.     *cell_data_comp = NULL,
  91.     buff[5], 
  92.     i,j,k,id,index,n, plflags = 0;
  93.     HPoint3 *verts;
  94.     int *nverts, *vertlist;
  95.     Point3 *normal = NULL;
  96.     ColorA *color = NULL;
  97.     ucdvert *ucdv;
  98.     ucdpoly *ucdp;
  99.     char str[UCD_NUMDATAFIELDS][32], label[UCD_NUMDATAFIELDS][32];
  100.     PolyList *mypl;
  101.  
  102.     {
  103.     char *timestring;
  104.     long mytime;
  105.     time_t myt;
  106.     myt = time(&mytime);
  107.     timestring = ctime(&myt);
  108.     fprintf(fp,"#  Created by ucdtooff on %s \n",timestring);
  109.     }
  110.  
  111.     {
  112.     int buff[5];
  113.     /* get the header; fgetni skips ucd comments  */
  114.     fgetni(fp,5,buff,0);
  115.     num_nodes = buff[0];
  116.     num_cells = buff[1];
  117.     num_node_data_comp = buff[2];
  118.     num_cell_data_comp = buff[3];
  119.     num_model_data = buff[4];
  120.     }
  121.  
  122.     ucdv  = OOGLNewN(ucdvert, num_nodes); 
  123.     verts = OOGLNewN(HPoint3, num_nodes); 
  124.     nverts = OOGLNewN(int, num_cells); 
  125.     ucdp = OOGLNewN(ucdpoly, num_cells); 
  126.     vertlist = OOGLNewN(int, 4*num_cells);     /* assume no face has more than 4 vertices */
  127.     if (num_node_data_comp)
  128.         node_data_comp = OOGLNewN(int, num_node_data_comp);
  129.     if (num_cell_data_comp)
  130.         cell_data_comp = OOGLNewN(int, num_cell_data_comp);
  131.  
  132.     /* read the vertices */
  133.     for (i=0; i<num_nodes; ++i)
  134.      {
  135.     fscanf(fp,"%d%g%g%g",&ucdv[i].id,&ucdv[i].v.x,&ucdv[i].v.y,&ucdv[i].v.z);
  136.     ucdv[i].v.w = 1.0;
  137.     }
  138.     /* translate the id's into indices */
  139.     for (i=0; i<num_nodes; ++i)    {
  140.     index = nodeidtoindex(ucdv[i].id, ucdv, num_nodes); 
  141.     if (index < 0) OOGLError(1,"Bad node id %d in ucdtooff\n",id);
  142.     verts[index] = ucdv[i].v;
  143.     }
  144.  
  145.     /* read the faces */
  146.     for (i=0; i<num_cells; ++i)    {
  147.     fscanf(fp,"%d",&ucdp[i].id);
  148.     fscanf(fp,"%d",&ucdp[i].m);    /* unused ? */
  149.     fscanf(fp,"%s",str);
  150.     ucdp[i].n = getcellsize(str);
  151.     for (j=0; j<ucdp[i].n; ++j)
  152.         fscanf(fp,"%d",&ucdp[i].vlist[j]);
  153.     }
  154.     /* translate the id's into indices */
  155.     {
  156.     int total;
  157.     for (total = 0, i=0; i<num_cells; ++i)    {
  158.     for (j=0; j<ucdp[i].n; ++j)    {
  159.         index = nodeidtoindex(ucdp[i].vlist[j], ucdv, num_nodes); 
  160.         if (index < 0) OOGLError(1,"Bad node id %d in ucdtooff\n",id);
  161.         nverts[i] = ucdp[i].n;
  162.         vertlist[total++] = nodeidtoindex(ucdp[i].vlist[j], ucdv, num_nodes); 
  163.         }
  164.     }
  165.     }
  166.  
  167.     /* theoretically, we could get a reasonable polylist at this point */
  168.     
  169.     /* get the node data descriptor */
  170.   if (num_node_data_comp != 0)  {
  171.     fscanf(fp,"%d", &num_node_data_comp);
  172.     for (i=0; i<num_node_data_comp; ++i)
  173.         fscanf(fp,"%d", &node_data_comp[i]);
  174.  
  175.     /* get the node labels */
  176.     for (i=0; i<num_node_data_comp; ++i)    {
  177.     fscanf(fp,"%s",str+i);
  178.     fscanf(fp,"%s",label+i);    /* this is ignored */
  179.     }
  180.  
  181.     /* get the node data */
  182.     for (i=0; i<num_node_data_comp; ++i)    {
  183.     n = strlen(str[i]);
  184.     /* get rid of commas */
  185.       if (str[i][n-1] == ',')  str[i][n-1] = '\0';
  186.     switch (gettype(str[i]))    {
  187.         case UCD_NORMAL:
  188.         if (node_data_comp[i] != 3)
  189.             OOGLError(1,"ucdtooff: bad normal descriptor\n");
  190.         plflags |= PL_HASVN;
  191.         normal = OOGLNewN(Point3, num_nodes);    
  192.         for (j=0;j<num_nodes; ++j)    {
  193.             fscanf(fp,"%d",&id);
  194.             index = nodeidtoindex(id, ucdv, num_nodes);
  195.             if (index < 0) OOGLError(1,"Bad node id %d in ucdtooff\n",id);
  196.             fscanf(fp,"%g%g%g",&normal[index].x,&normal[index].y,&normal[index].z);
  197.             }
  198.         break;
  199.  
  200.         case UCD_RGBA:
  201.         if (node_data_comp[i] != 4)
  202.             OOGLError(1,"ucdtooff: bad colora descriptor\n");
  203.         plflags |= PL_HASVCOL;
  204.         color = OOGLNewN(ColorA, num_nodes);    
  205.         for (j=0;j<num_nodes; ++j)    {
  206.             fscanf(fp,"%d",&id);
  207.             index = nodeidtoindex(id, ucdv, num_nodes);
  208.             if (index < 0) OOGLError(1,"Bad node id %d in ucdtooff\n",id);
  209.             fscanf(fp,"%g%g%g%g",&color[index].r,&color[index].g,&color[index].b, &color[index].a);
  210.             } 
  211.         break;
  212.  
  213.         case UCD_RGB:
  214.         if (node_data_comp[i] != 3)
  215.             OOGLError(1,"ucdtooff: bad color descriptor\n");
  216.         plflags |= PL_HASVCOL;
  217.         color = OOGLNewN(ColorA, num_nodes);    
  218.         for (j=0;j<num_nodes; ++j)    {
  219.             fscanf(fp,"%d",&id);
  220.             index = nodeidtoindex(id, ucdv, num_nodes);
  221.             if (index < 0) OOGLError(1,"Bad node id %d in ucdtooff\n",id);
  222.             fscanf(fp,"%g%g%g",&color[index].r,&color[index].g,&color[index].b);
  223.             color[index].b = 1.0;
  224.             }
  225.         break;
  226.         default:
  227.         {
  228.         float tt;
  229.         fprintf(stderr,"ucdtooff: unknown type %s\n",str[i]);
  230.         /* skip over the data */
  231.         for (j=0;j<num_nodes; ++j)    {
  232.             fscanf(fp,"%d",&id);
  233.             for (k=0;k<node_data_comp[i]; ++k)    
  234.             fscanf(fp,"%g",&tt);
  235.             }
  236.         }
  237.         break;
  238.         }    
  239.     }
  240.     }
  241.  
  242.     /* get the cell data descriptor */
  243.   if (num_cell_data_comp != 0)  {
  244.     fscanf(fp,"%d", &num_cell_data_comp);
  245.     for (i=0; i<num_cell_data_comp; ++i)
  246.         fscanf(fp,"%d", &cell_data_comp[i]);
  247.  
  248.     /* get the cell labels */
  249.     for (i=0; i<num_cell_data_comp; ++i)    {
  250.     fscanf(fp,"%s",str+i);
  251.     fscanf(fp,"%s",label+i);    /* this is ignored */
  252.     }
  253.  
  254.     /* get the cell data */
  255.     for (i=0; i<num_cell_data_comp; ++i)    {
  256.     n = strlen(str[i]);
  257.     /* get rid of commas */
  258.       if (str[i][n-1] == ',')  str[i][n-1] = '\0';
  259.     switch (gettype(str[i]))    {
  260.         case UCD_NORMAL:
  261.         if (cell_data_comp[i] != 3)
  262.             OOGLError(1,"ucdtooff: bad normal descriptor\n");
  263.         plflags |= PL_HASPN;
  264.         normal = OOGLNewN(Point3, num_cells);    
  265.         for (j=0;j<num_cells; ++j)    {
  266.             fscanf(fp,"%d",&id);
  267.             index = cellidtoindex(id, ucdp, num_cells);
  268.             if (index < 0) OOGLError(1,"Bad cell id %d in ucdtooff\n",id);
  269.             fscanf(fp,"%g%g%g",&normal[index].x,&normal[index].y,&normal[index].z);
  270.             }
  271.         break;
  272.  
  273.         case UCD_RGBA:
  274.         if (cell_data_comp[i] != 4)
  275.             OOGLError(1,"ucdtooff: bad colora descriptor\n");
  276.         plflags |= PL_HASPCOL;
  277.         color = OOGLNewN(ColorA, num_cells);    
  278.         for (j=0;j<num_cells; ++j)    {
  279.             fscanf(fp,"%d",&id);
  280.             index = cellidtoindex(id, ucdp, num_cells);
  281.             if (index < 0) OOGLError(1,"Bad cell id %d in ucdtooff\n",id);
  282.             fscanf(fp,"%g%g%g%g",&color[index].r,&color[index].g,&color[index].b, &color[index].a);
  283.             } 
  284.         break;
  285.  
  286.         case UCD_RGB:
  287.         if (cell_data_comp[i] != 3)
  288.             OOGLError(1,"ucdtooff: bad color descriptor\n");
  289.         plflags |= PL_HASPCOL;
  290.         color = OOGLNewN(ColorA, num_cells);    
  291.         for (j=0;j<num_cells; ++j)    {
  292.             fscanf(fp,"%d",&id);
  293.             index = cellidtoindex(id, ucdp, num_cells);
  294.             if (index < 0) OOGLError(1,"Bad cell id %d in ucdtooff\n",id);
  295.             fscanf(fp,"%g%g%g",&color[index].r,&color[index].g,&color[index].b);
  296.             color[index].b = 1.0;
  297.             }
  298.         break;
  299.         default:
  300.         {
  301.         float tt;
  302.         fprintf(stderr,"ucdtooff: unknown type %s\n",str[i]);
  303.         /* skip over the data */
  304.         for (j=0;j<num_cells; ++j)    {
  305.             fscanf(fp,"%d",&id);
  306.             for (k=0;k<cell_data_comp[i]; ++k)    
  307.             fscanf(fp,"%g",&tt);
  308.             }
  309.         }
  310.         break;
  311.         }    
  312.     }
  313.     }
  314.  
  315.     {
  316.     int cflag, nflag;
  317.     Geom *mypl;
  318.  
  319.     if (plflags & PL_HASPCOL)  cflag = CR_POLYCOLOR;
  320.     else if (plflags & PL_HASVCOL) cflag = CR_COLOR;
  321.     else cflag = CR_COLOR;
  322.  
  323.     if (plflags & PL_HASPN)  nflag = CR_POLYNORMAL;
  324.     else if (plflags & PL_HASVN) nflag = CR_NORMAL;
  325.     else nflag = CR_NORMAL;
  326.  
  327.     mypl = GeomCreate("polylist",
  328.                 /*CR_NOCOPY,            isn't supported for polylists! */
  329.                 CR_NPOLY, num_cells,
  330.                 CR_NVERT, nverts,
  331.                 CR_VERT,  vertlist,
  332.                 CR_POINT4, verts,
  333.                 cflag, color,
  334.                 nflag, normal,
  335.                 CR_FLAG, plflags, CR_END
  336.          );
  337.     GeomFSave(mypl, stdout, NULL);
  338.     }
  339. }
  340.  
  341.  
  342.